/**
 * https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/
 * 两个数组的交集II
 * @param nums1 
 * @param nums2 
 */
const intersect = (nums1: number[], nums2: number[]): number[] => {
    const map: Record<number, number> = {}
    const n1Len = nums1.length,
        n2Len = nums2.length,
        ans = []


    // 遍历1, 存入map中 
    for (let i = 0; i < n1Len; i++) {
        // 存的是元素出现的次数
        let curr = nums1[i];
        map[curr] = map[curr] || 0
        map[curr]++
    }

    // 遍历nums2, 找出交集
    for (let i = 0; i < n2Len; i++) {
        if (map[nums2[i]]-- > 0) {
            ans.push(nums2[i])

        }
    }

    return ans
};

console.log(intersect([4, 9, 5, 5, 9, 9], [9, 4, 8, 4, 5, 9]))